package com.zzccyy.jpdemo.utils;

import com.zzccyy.jpdemo.bean.DateInfo;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 日期工具类
 */
public class DateUtils {
    
    /**
     * 将中文日期转换为LocalDate对象
     */
    public static LocalDate parseChineseDate(String chineseDate) {
        String[] parts = chineseDate.split("[年月日]");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);
        int day = Integer.parseInt(parts[2]);
        return LocalDate.of(year, month, day);
    }
    
    /**
     * 将LocalDate转换为中文日期格式
     */
    public static String formatToChineseDate(LocalDate date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日", Locale.CHINA);
        return date.format(formatter);
    }
    
    /**
     * 将星期几转换为"周几"格式
     */
    public static String convertToZhouFormat(DayOfWeek dayOfWeek) {
        switch (dayOfWeek) {
            case MONDAY:
                return "周一";
            case TUESDAY:
                return "周二";
            case WEDNESDAY:
                return "周三";
            case THURSDAY:
                return "周四";
            case FRIDAY:
                return "周五";
            case SATURDAY:
                return "周六";
            case SUNDAY:
                return "周日";
            default:
                return "";
        }
    }
    
    /**
     * 获取从指定日期开始的连续多天日期信息（包含当天）
     * @param chineseDate 起始日期（中文格式）
     * @param totalDays 总天数（包含起始日）
     * @return 日期信息列表
     */
    public static List<DateInfo> getDateRangeInfo(String chineseDate, int totalDays) {
        List<DateInfo> result = new ArrayList<>();
        LocalDate startDate = parseChineseDate(chineseDate);
        
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        
        for (int i = 0; i < totalDays; i++) {
            LocalDate currentDate = startDate.plusDays(i);
            
            // 格式化日期
            String formattedDate = currentDate.format(dateFormatter);
            String formattedChineseDate = formatToChineseDate(currentDate);
            
            // 获取星期几并转换为"周几"格式
            DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
            String weekDay = convertToZhouFormat(dayOfWeek);
            
            // 创建DateInfo对象
            DateInfo dateInfo = new DateInfo(formattedDate, weekDay, formattedChineseDate, i);
            result.add(dateInfo);
        }
        
        return result;
    }
    
    /**
     * 获取从今天开始的连续多天日期信息
     * @param totalDays 总天数（包含今天）
     * @return 日期信息列表
     */
    public static List<DateInfo> getDateRangeInfoFromToday(int totalDays) {
        LocalDate today = LocalDate.now();
        String todayChinese = formatToChineseDate(today);
        return getDateRangeInfo(todayChinese, totalDays);
    }
    
    /**
     * 获取指定日期后N天的日期信息
     * @param chineseDate 起始日期
     * @param daysAfter 多少天后
     * @return 日期信息对象
     */
    public static DateInfo getDateAfterDays(String chineseDate, int daysAfter) {
        LocalDate startDate = parseChineseDate(chineseDate);
        LocalDate targetDate = startDate.plusDays(daysAfter);
        
        // 格式化日期
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formattedDate = targetDate.format(dateFormatter);
        String formattedChineseDate = formatToChineseDate(targetDate);
        
        // 获取星期几并转换为"周几"格式
        DayOfWeek dayOfWeek = targetDate.getDayOfWeek();
        String weekDay = convertToZhouFormat(dayOfWeek);
        
        return new DateInfo(formattedDate, weekDay, formattedChineseDate, daysAfter);
    }
}